<template>
  <div>
    <!-- 地址添加页 -->
    <van-address-edit
      :area-list="areaList"

      show-delete
      show-set-default
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      @save="onSave"
 
    >
      <div class="van-cell van-field">
        <div class="van-cell__title van-field__label">
          <span>标签</span>
        </div>
        <div class="van-cell__value van-field__value">
          <div class="van-field__body">
            <van-radio-group v-model="label" direction="horizontal">
              <van-radio name="家">家</van-radio>
              <van-radio name="公司">公司</van-radio>
            <van-radio name="学校">学校</van-radio>
            </van-radio-group>
          </div>
        </div>
      </div>
    </van-address-edit>
  </div>
</template>
<script>
import { reqAddressAdd, reqAddressEdit, reqAddressList, reqshopedit } from "../../http/http";
import { areaList } from "@vant/area-data";
import { Toast } from "vant";
export default {
  data() {
    return {
      areaList: areaList,
      label: "",
      info: {
        name: "",
        tel: "",
        province: "",
        city: "",
        county: "",
        addressDetail: "",
        areaCode: "",
        isDefault: false,
      },
    };
  },
  mounted() {
    //重置info
    this.info = {
      name: "",
      tel: "",
      province: "",
      city: "",
      county: "",
      addressDetail: "",
      areaCode: "",
      isDefault: false,
    };
    this.label = "";

    //如果有id这个字段，代表作编辑
    if (this.$route.query.id) {
      reqAddressList({
        id: this.$route.query.id,
        uid: JSON.parse(localStorage.getItem("userInfo")).uid,
      }).then((res) => {
        /* {"id":37,"uid":"240014a0-57cd-11ec-854e-4d940a68a7f2",
          "username":"zs","userphone":"13411112222","regioncode":"130203",
          "location":"河北省唐山市路北区","useraddress":"22","status":1,"label":"公司"}*/

        if (res.data.code === 200) {
          let message = res.data.list[0];
          this.info = {
            id: message.id,
            name: message.username,
            tel: message.userphone,

            addressDetail: message.useraddress,
            areaCode: message.regioncode,
            isDefault: message.status == 1,
          };
          this.label = message.label;
        }
      });
    }
  },
  methods: {
    onSave(e) {
      if (this.$route.query.id) {
        //修改

        reqAddressEdit({
          id: this.$route.query.id,
          username: e.name,
          userphone: e.tel,
          regioncode: e.areaCode,
          location: e.province + e.city + e.county,
          useraddress: e.addressDetail,
          status: e.isDefault ? 1 : 0,
          label: this.label,
        }).then((res) => {
          if (res.data.code === 200) {
            //弹成功
            Toast(res.data.msg);
            // 跳转到列表
            this.$router.replace("/adress");
          }
        });
      } else {
        /*e:{"name":"hlt","tel":"15733334444","country":"",
      "province":"天津市","city":"天津市","county":"河东区","areaCode":"120102",
      "postalCode":"","addressDetail":"1号院","isDefault":true}
      */
        //发添加请求
        reqAddressAdd({
          uid: JSON.parse(localStorage.getItem("userInfo")).uid,
          username: e.name,
          userphone: e.tel,
          regioncode: e.areaCode,
          location: e.province + e.city + e.county,
          useraddress: e.addressDetail,
          status: e.isDefault ? 1 : 0,
          label: this.label,
        }).then((res) => {
          if (res.data.code === 200) {
            //弹成功
            Toast(res.data.msg);
            // 跳转到列表
            this.$router.replace("/adress");
          }
        });
      }
    },
  },
};
</script>